home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 272_01 / testints.c < prev    next >
Text File  |  1986-10-12  |  4KB  |  113 lines

  1. /*
  2. **          TEST PROGRAM FOR INTERRUPT HANDLER FUNCTIONS
  3. **  This program demonstrates the installable interrupt
  4. **  service routines contained in the functions ticker, ctlbrk(),
  5. **  and in SMDLx.LIB.
  6. **  The ticker routine installs on
  7. **  interrupt 1CH, and chains after completion to whatever
  8. **  routine may have already been hung on that interrupt.
  9. **  Ticker is not a C function as such, but a stand-alone assembly
  10. **  language routine which is installed by installtick() and removed
  11. **  by removetick().
  12. **  The ctlbrk() routine is used to exit the program and cleanup
  13. **  the interrupt vector table upon exit.  It installs upon int 23H.
  14. **
  15. **  Copyright 1986, S.E. Margison
  16. **
  17. */
  18.  
  19. #include <stdio.h>
  20. #include <dos.h>
  21. #include <smdefs.h>
  22.  
  23. /* first, we must declare our control-break handler routine.
  24. ** this routine could do almost anything, but we will just
  25. ** set a flag that the main program can test, and print a message.
  26. ** Note that this should be declared BEFORE main() routine!
  27. */
  28.  
  29. int killit;
  30.  
  31. handler() {    /* handler cannot return a value, so declare void */
  32.    killit = YES;
  33.    puts("Control-C received; exiting program");
  34.    return(0);       /* return 0 to continue program */
  35.    }
  36.  
  37. int count;       /* this is the variable which ticker will manipulate */
  38. extern void installtick(), removetick();
  39.  
  40. main() {
  41.    int i;
  42.  
  43. /* BEFORE we change the interrupt vector table, we MUST
  44. ** install our control-C trap.  If not, it would be possible
  45. ** for a fast typist to control-C out of the program after
  46. ** we mess up the vector table and before we grab the control-C
  47. ** handler vector.   Result:  Well, just turn off the power switch
  48. ** and start all over again!!!
  49. */
  50.  
  51.    ctlbrk(&handler);       /* install our trap */
  52.    killit = NO;            /* and clear our trap flag */
  53.  
  54.  
  55.  
  56. /* Use installtick() to pass the desired variable address to the ticker
  57. ** process and install it upon the interrupt.
  58. */
  59.  
  60.    installtick(&count);
  61.  
  62.  
  63.  
  64. /* this loop demonstrates the use of the ticker function.  As long as
  65. ** the count variable is not 0, it will be decremented 18.21 times
  66. ** per second.  ticker tests the value for zero and if it is,
  67. ** does not decrement it again.  This makes it unnecessary to try to
  68. ** "trap" count exactly at zero before the next interrupt occurs.
  69. ** To use ticker, place a value in count (18 = 1 second more or less).
  70. ** Then, check it every so often to see if it is zero (or whatever
  71. ** your heart desires).
  72. */
  73.  
  74.    i = 0;
  75.    while(!killit) {          /* abort when control-C trap sets this flag */
  76.       count = 18;            /* set a value of 1 second */
  77.       while(count != 0);     /* loop until timed out */
  78.       printf("This is loop #%d\n", i++);  /* do something useful here */
  79.       }                      /* and go again */
  80.  
  81. /* if we are here, then our control-C trap worked! */
  82.  
  83. /* now, we have to clean up after ourselves.  It is necessary
  84. ** to restore the original vector table contents.  Alternate
  85. ** option is to turn off the power!!
  86. */
  87.  
  88. /* first, remove the ticker from the interrupt table.
  89. ** WARNING:  FAILURE TO REMOVE TICKER BEFORE EXITING PROGRAM
  90. **           WILL DEFINITELY (as in positively!) CRASH THE
  91. **           SYSTEM ALMOST IMMEDIATELY!!!!!!
  92. */
  93.     removetick();
  94.  
  95. /* finally, let's restore the original control-C handler by uninstalling
  96. ** our own.  A NULL parameter does the job!
  97. ** Yes, DOS restores the original vector upon exit from our program, but
  98. ** this shows that we can also do that ourselves.
  99. */
  100.    ctlbrk(0);
  101.  
  102.    }
  103.  
  104. /* If using ticker, the following ctlbrk handler would be sufficient
  105. ** if it was desired to abort the program cleanly when a control-break
  106. ** was pressed:
  107.    handler {
  108.       removetick();
  109.       return(1);
  110.       }
  111. */
  112.  
  113.